﻿using System;
using System.Collections.Generic;
using DMS.Business.Utility;

namespace DMS.Business.Modules.Organization
{
    /// <summary>
    /// 角色
    /// </summary>
    public class Role : Actor
    {
        #region 数据成员/内部成员
        protected string _Description;
        protected RoleTypes _Type = RoleTypes.Normal;
        protected bool _IsBuildIn = false;
        protected bool _IsDisplay = true;

        protected List<Actor> _Members = null;
        #endregion

        #region 成员属性
        public string MemberScopeName { get; set; }

        public override ActorTypes ActorType
        {
            get
            {
                return ActorTypes.Role;
            }
            set
            {
            }
        }
        /// <summary>
        /// 角色描述
        /// </summary>
        public string Description
        {
            get
            {
                return this._Description;
            }
            set
            {
                this._Description = value;
            }
        }
        /// <summary>
        /// 角色类型
        /// </summary>
        /// <remarks>
        /// 0:管理角色,1:流程角色，2:特殊角色
        /// </remarks>
        public virtual RoleTypes Type
        {
            get
            {
                return this._Type;
            }
            set
            {
                this._Type = value;
            }
        }
        /// <summary>
        /// 是否内置角色
        /// </summary>
        public bool IsBuildIn
        {
            get
            {
                return this._IsBuildIn;
            }
            internal set
            {
                this._IsBuildIn = value;
            }
        }
        /// <summary>
        /// 是否显示
        /// </summary>
        public bool IsDisplay
        {
            get
            {
                return this._IsDisplay;
            }
            internal set
            {
                this._IsDisplay = value;
            }
        }

        /// <summary>
        /// 角色成员
        /// </summary>
        public List<Actor> Members
        {
            get
            {
                if (this._Members == null)
                {
                    this._Members = UserHelper.GetRoleMembers(this);
                }

                return this._Members;
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 创建角色对象
        /// </summary>
        /// <param name="id">角色唯一编码</param>
        public Role(string id)
            : base(id)
        {
        }
        public Role()
        {
        }
        #endregion

        #region 成员方法
        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            if (string.IsNullOrEmpty(_ID))
            {
                this._ID = Guid.NewGuid().ToString();
                UserHelper.AddRole(this);
            }
            else
            {
                UserHelper.UpdateRole(this);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        public void Delete()
        {
            if (string.IsNullOrEmpty(_ID))
            {
                throw new InvalidOperationException("删除失败，该角色还未保存，无法删除。");
            }

            UserHelper.DeleteRole(this);
        }
        #endregion
    }
}
